﻿using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Coldairarrow.Business.PB
{
    public partial class PB_WorkStationBusiness : BaseBusiness<PB_WorkStation>, IPB_WorkStationBusiness, ITransientDependency
    {
        public PB_WorkStationBusiness(IDbAccessor db)
            : base(db)
        {
        }

        #region 外部接口

        public async Task<PageResult<PB_WorkStationDTO>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            //var q = GetIQueryable();
            //var where = LinqHelper.True<PB_WorkStation>();
            //var search = input.Search;

            ////筛选
            //if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            //{
            //    var newWhere = DynamicExpressionParser.ParseLambda<PB_WorkStation, bool>(
            //        ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
            //    where = where.And(newWhere);
            //}

            //return await q.Where(where).GetPageResultAsync(input);
            var search = input.Search;
            Expression<Func<PB_WorkStation, Base_Department, PB_Workshop, PB_WorkStationDTO>> select = (a, b, c) => new PB_WorkStationDTO
            {
                DepartmentName = b.Name,
                WorkshopName = c.Name
            };
            select = select.BuildExtendSelectExpre();
            var q = from a in GetIQueryable().AsExpandable()
                    join b in Db.GetIQueryable<Base_Department>() on a.DeptId equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    join c in Db.GetIQueryable<PB_Workshop>() on a.WorkshopId equals c.Id into ac
                    from c in ac.DefaultIfEmpty()
                    select @select.Invoke(a, b, c);

            if (!search.Keyword.IsNullOrEmpty())
            {
                var keyword = $"%{search.Keyword}%";
                q = q.Where(x =>
                      EF.Functions.Like(x.Name, keyword)
                      || EF.Functions.Like(x.Code, keyword));
            }
          
            return await q.GetPageResultAsync(input);
        }

        public async Task<PageResult<PB_WorkStation>> GetDataListByWorkshopAsync(string workshopId)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<PB_WorkStation>();

            //筛选
            if (!workshopId.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<PB_WorkStation, bool>(
                    ParsingConfig.Default, false, $@"WorkshopId.Contains(@0)", workshopId);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(new PageInput());
        }

        public async Task<PB_WorkStation> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        [DataRepeatValidate(new string[] { "Code" }, new string[] { "工作站编号" })]
        public async Task AddDataAsync(PB_WorkStation data)
        {
            var abcObject = new
            {
                //标识
                Key = "ZY0008",
                //json
                Value = new
                {
                    Id = data.Id,
                    DeptId = data.DeptId,
                    WorkshopId = data.WorkshopId
                }
            };
            data.QrCode = abcObject.ToString();
            await InsertAsync(data);
        }

        [DataRepeatValidate(new string[] { "Code" }, new string[] { "工作站编号" })]
        public async Task UpdateDataAsync(PB_WorkStation data)
        {
            var abcObject = new
            {
                //标识
                Key = "ZY0008",
                //json
                Value = new
                {
                    Id = data.Id,
                    DeptId = data.DeptId,
                    WorkshopId = data.WorkshopId
                }
            };
            data.QrCode = abcObject.ToString();
            //data.QrCode = Extention.Base64Encode(JsonConvert.SerializeObject(abcObject));
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        #endregion

        #region 私有成员

        #endregion
    }
}